//
//  DHEvaluateCell.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/11.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHEvaluateCell.h"
@interface DHEvaluateCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView; // 
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIView *evaStarView;
@property (weak, nonatomic) IBOutlet UILabel *evaContentView;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *picHeightCons;
@property (weak, nonatomic) IBOutlet UIView *picView;
@property(nonatomic ,strong)HYBStarEvaluationView *starView;
@end
@implementation DHEvaluateCell

- (void)setModel:(DHEvaluateModel *)model{
    _model = model;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:model.from_avatar]];
    self.nameLabel.text = model.from_name;
    self.timeLabel.text = model.addtime;
    self.evaContentView.text = model.content;
    
    for (int i = 0; i < model.imageList.count; i++) {
        
        UIImageView *iv = self.picView.subviews[i];
        iv.hidden = false;
        [iv sd_setImageWithURL:[NSURL URLWithString:model.imageList[i]]];
    }
    
    if (!(model.imageList.count > 0)) {
        self.picHeightCons.constant = 0;
        [self.contentView layoutIfNeeded];
    }
    
    self.starView.actualScore = model.scores.floatValue;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupEvaPicView];
    
    self.starView = [[HYBStarEvaluationView alloc]initWithFrame:self.evaStarView.bounds numberOfStars:5 isVariable:YES];
    
    self.starView.isContrainsHalfStar = YES;//是否包含半颗星，默认为NO
    self.starView.fullScore = 5;
    [self.evaStarView addSubview:self.starView];
}

#pragma mark: - 创建图片
- (void)setupEvaPicView{
    
    //计算评论照片尺寸
    CGFloat margin = 3;
    CGFloat singleImageW = (self.picView.bounds.size.width - 4 * margin) / 6;
    
    //遍历创建评论照片
    for (int i = 0 ; i < 5; i++) {
        
        UIImageView *iv = [[UIImageView alloc] init];
        iv.hidden = true;
        iv.frame = CGRectMake(i * (singleImageW + margin),
                              0,
                              singleImageW,
                              self.picView.bounds.size.height);
        //iv.backgroundColor = UIColor.yellowColor;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPhoto:)];
        [iv addGestureRecognizer:tap];
        iv.tag = i;
        iv.userInteractionEnabled = true;
        [self.picView addSubview:iv];
    }
}
#pragma mark: - 评价图像点击监听
- (void)showPhoto:(UITapGestureRecognizer *)tap
{
    if (self.model.imageList.count == 0) {return;}
    UIImageView *imgView = (UIImageView *)tap.view;
    //点击的图像索引
    NSInteger idx = imgView.tag;
    
    NSMutableArray<UIImageView *> *ivList = [NSMutableArray array];
    for (UIImageView *iv in self.picView.subviews)
    {
        if (!iv.isHidden) {[ivList addObject:iv];}
    }
    
    //获取图像url数组
    NSArray *imgUrlList = self.model.imageList;
    
    [NSNotificationCenter.defaultCenter postNotificationName:kCommentPhotoBrowerNotification
                                                      object:nil
                                                    userInfo:@{@"imgViewIdx":@(idx),
                                                               @"imgUrl":imgUrlList,
                                                               @"ivList":ivList
                                                               }];
    
}
@end
